<html>
<head><meta charset="utf-8"><title>Assertions with min_const_generics · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assertions.20with.20min_const_generics.html">Assertions with min_const_generics</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="231524020"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assertions%20with%20min_const_generics/near/231524020" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> John Gallagher <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assertions.20with.20min_const_generics.html#231524020">(Mar 23 2021 at 18:56)</a>:</h4>
<p>Is it possible to have compile-time assertions on const generic values in beta? E.g.,</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">foo</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="fm">assert!</span><span class="p">(</span><span class="n">N</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">10</span><span class="p">);</span><span class="w"> </span><span class="c1">// make this a compile-time check instead of runtime</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="231524211"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assertions%20with%20min_const_generics/near/231524211" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assertions.20with.20min_const_generics.html#231524211">(Mar 23 2021 at 18:57)</a>:</h4>
<p>not with min_const_generics, no</p>



<a name="231524227"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assertions%20with%20min_const_generics/near/231524227" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assertions.20with.20min_const_generics.html#231524227">(Mar 23 2021 at 18:57)</a>:</h4>
<p>not sure if it's planned for <code>const_generics</code></p>



<a name="231524464"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assertions%20with%20min_const_generics/near/231524464" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> John Gallagher <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assertions.20with.20min_const_generics.html#231524464">(Mar 23 2021 at 18:59)</a>:</h4>
<p>shucks. thanks for the quick answer :)</p>



<a name="231525348"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assertions%20with%20min_const_generics/near/231525348" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assertions.20with.20min_const_generics.html#231525348">(Mar 23 2021 at 19:05)</a>:</h4>
<p>yes, it's possible with associated consts tricks like <a href="https://play.rust-lang.org/?version=beta&amp;mode=debug&amp;edition=2018&amp;gist=1add07d188960dcb18dd305d8380ccb5">https://play.rust-lang.org/?version=beta&amp;mode=debug&amp;edition=2018&amp;gist=1add07d188960dcb18dd305d8380ccb5</a> but that comes with subpar errors for now</p>



<a name="231525777"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assertions%20with%20min_const_generics/near/231525777" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assertions.20with.20min_const_generics.html#231525777">(Mar 23 2021 at 19:07)</a>:</h4>
<p>see for example what's done in stdarch for some of these, including using only one such struct (rather than one per macro invocation) to limit the MIR sizes, compile times, and so on: <a href="https://github.com/rust-lang/stdarch/blob/6495bb0e33578443c21764655a4dd8b55399c008/crates/core_arch/src/macros.rs">https://github.com/rust-lang/stdarch/blob/6495bb0e33578443c21764655a4dd8b55399c008/crates/core_arch/src/macros.rs</a></p>



<a name="231526287"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assertions%20with%20min_const_generics/near/231526287" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assertions.20with.20min_const_generics.html#231526287">(Mar 23 2021 at 19:10)</a>:</h4>
<p>oh that is an <em>excellent</em> hack, I love it <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span></p>



<a name="231526589"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assertions%20with%20min_const_generics/near/231526589" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hyd-dev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assertions.20with.20min_const_generics.html#231526589">(Mar 23 2021 at 19:12)</a>:</h4>
<p>I recall this now: <a class="stream-topic" data-stream-id="146212" href="/#narrow/stream/146212-t-compiler.2Fconst-eval/topic/static_assert.20for.20const.20generics">#t-compiler/const-eval &gt; static_assert for const generics</a></p>



<a name="231526616"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assertions%20with%20min_const_generics/near/231526616" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assertions.20with.20min_const_generics.html#231526616">(Mar 23 2021 at 19:12)</a>:</h4>
<p>(IIRC if you use such tricks with multiple crates, you don't even see the code where the error happens, so it's probably best to wait until error reporting is improved here)</p>



<a name="231526636"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assertions%20with%20min_const_generics/near/231526636" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assertions.20with.20min_const_generics.html#231526636">(Mar 23 2021 at 19:12)</a>:</h4>
<p>that's the one</p>



<a name="231526892"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assertions%20with%20min_const_generics/near/231526892" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assertions.20with.20min_const_generics.html#231526892">(Mar 23 2021 at 19:13)</a>:</h4>
<p>you can also do similar tricks to cast consts, which is otherwise not super easy to do with <code>min_const_generics</code> sometimes</p>



<a name="231528367"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assertions%20with%20min_const_generics/near/231528367" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assertions.20with.20min_const_generics.html#231528367">(Mar 23 2021 at 19:22)</a>:</h4>
<p>there should really be a const_assert in the standard library, at least in nightly</p>



<a name="231534485"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Assertions%20with%20min_const_generics/near/231534485" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Assertions.20with.20min_const_generics.html#231534485">(Mar 23 2021 at 20:01)</a>:</h4>
<p><code>assert!</code> works in constants if you enable <code>#![feature(const_panic)]</code></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>